﻿using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace Chimp.Core.PostFX
{
    public class PostFX_Static : ChimpPostFX
    {
        private readonly Random rnd = new Random();
        public int ChangeRate = 25;
        public ChimpTexture2D ColorMap;
        public ChimpRenderTarget EffectRT2 = new ChimpRenderTarget();
        public ChimpRenderTarget EffectRT3 = new ChimpRenderTarget();
        public int NextChange;
        public float StaticLevel;
        public ChimpTexture2D StaticMap;

        public PostFX_Static()
        {
            Effect = new ChimpEffect("", "Res/Shader/PostFX_Static.glsl");
            Effect.Use();
            Effect.SetInt("SourceMap", 0);
            Effect.SetInt("StaticMap", 1);
            Effect.SetInt("ColorMap", 2);
            Effect.Fin();
            StaticMap = new ChimpTexture2D(256, 256);
            ColorMap = new ChimpTexture2D(256, 256);
        }

        public override void OnPreDrawPostFX(ChimpTexture2D tex)
        {
            UpdateFX();
            //base.OnPreDrawPostFX(tex);
        }

        public override void OnDrawPostFX(ChimpTexture2D src)
        {
            Effect.Use();
            Effect.SetFloat("StaticLevel", StaticLevel);
            Effect.SetInt("SourceMap", 0);
            Effect.SetInt("StaticMap", 1);
            Effect.SetInt("ColorMap", 2);
            StaticMap.Bind(1);
            ColorMap.Bind(2);
            ChimpDraw.BlendMode = ChimpBlend.Lighten;
            ChimpDraw.Image(new Vector4(0, 0, ChimpApp.W, ChimpApp.H), src, new Vector4(1, 1, 1, 1));
            ColorMap.Unbind(2);
            StaticMap.Unbind(1);
            Effect.Fin();
        }

        private void UpdateFX()
        {
            int t = Environment.TickCount;
            if (t > NextChange)
            {
                NextChange = t + ChangeRate;
            }
            else
            {
                return;
            }

            EffectRT2.Bind();
            EffectRT2.AttachTexture(StaticMap);
            ChimpDraw.BlendMode = ChimpBlend.Alpha;
            for (int i = 0; i < StaticMap.Height; i += 2)
            {
                for (int j = 0; j < StaticMap.Width; j += 6)
                {
                    var xo = (float) rnd.NextDouble();
                    var yo = (float) rnd.NextDouble();

                    ChimpDraw.Rect(new Vector4(j, ChimpApp.H - i, 6, 2), new Vector4(xo, yo, 0, 1));
                }
            }
            EffectRT2.Unbind();
            EffectRT3.Bind();
            EffectRT3.AttachTexture(ColorMap);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            ChimpDraw.BlendMode = ChimpBlend.Alpha;
            for (int i = 0; i < ColorMap.Height; i += 1)
            {
                float xo = (float) rnd.NextDouble()*0.35f;

                ChimpDraw.Rect(new Vector4(0, ChimpApp.H - i, ColorMap.Width, 8),
                               new Vector4((float) rnd.NextDouble(), (float) rnd.NextDouble(), (float) rnd.NextDouble(),
                                           1.0f));
            }
            EffectRT3.Unbind();
        }
    }
}